package tech.autumn.exception.resolver;

import org.springframework.web.servlet.ModelAndView;
import tech.autumn.spec.exception.BusinessException;
import tech.autumn.spec.web.Response;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 业务异常处理器
 * @author koko
 * @date 2025年01月21日 15:55
 */
public class HandlerBusinessExceptionResolver extends AbstractHandlerExceptionJsonResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
                                         Exception ex) {
        if (ex instanceof BusinessException) {
            BusinessException be = (BusinessException) ex;
            return businessFail(be.getCode(), be.getMsg());
        }
        return new ModelAndView(this.jsonView, Response.fail(500).msg("服务异常").data(ex.getMessage()));
    }

}
